package com.iplant.ams.serviceImpl.thread;

import com.iplant.ams.service.po.dms.DMSDeviceLedger;
import com.iplant.ams.serviceImpl.AMSServiceImpl;
import com.iplant.ams.serviceImpl.DRMServiceImpl;
import com.iplant.base.utils.cache.CacheUtils;
import com.iplant.base.utils.general.CalendarTool;
import com.iplant.base.utils.tasker.TaskTimerUtils;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class AMSThread implements DisposableBean {
    private static final Logger logger = LoggerFactory.getLogger(AMSThread.class);


    @PostConstruct
    public void init() {
        Run();
    }

    private void Run() {
        new Thread(() -> {
            try {
                Thread.sleep(10000);
                //初始化资产分类，只执行一次
                AMSServiceImpl.getInstance().AMS_InitAssetClassification(BMSEmployee.getSysAdmin());
                //初始化设备构型，只执行一次
                DRMServiceImpl.getInstance().DRM_InitConfiguration(BMSEmployee.SysAdmin);

                //自动触发点检、维保任务（轮询执行）
                TaskTimerUtils.AddTask(() ->
                                DRMServiceImpl.getInstance().DMS_AutoTriggerTask(BMSEmployee.getSysAdmin()),
                        CalendarTool.getDate(),
                        CalendarTool.getDateMaxMill(), 5000, -1);
            } catch (Exception ex) {
                logger.error("ApplicationThread", ex);
            }
        }).start();
    }

    @Override
    public void destroy() {

        TaskTimerUtils.destroy();

    }
}
